package cn.jsprun.taglib;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


public class ShowTime extends TagSupport {

	private static final long serialVersionUID = -7195275797128947964L;
	private int timeInt;
	private boolean isView = true;
	private boolean isShowDefault=false;
	private String type = null;
	private long timeoffset=0;
	public void setTimeoffset(float timeoffset) {
		this.timeoffset = (long)(timeoffset*3600);
	}
	public void setTimeInt(Integer timeInt){
		this.timeInt = timeInt;
	}
	public void setIsView(String isView){
		if(isView!=null && isView.equals("false")){
			this.isView = false;
		}else{
			this.isView = true;
		}
	}
	public void setIsShowDefault(String isShowDefault){
		if(isShowDefault!=null && isShowDefault.equals("true")){
			this.isShowDefault = true;
		}else{
			this.isShowDefault = false;
		}
	}
	public void setType(String type){
		if(type.equals("")){
			this.type = "yyyy-MM-dd";
		}else{
			this.type = type;
		}
	}
	@Override
	public int doStartTag() throws JspException {
		if(this.type==null){
			this.type = "yyyy-MM-dd";
		}
		long timeLong =(timeInt+timeoffset)*1000l;
		try {
			if(timeInt>0)
			{
				pageContext.getOut().write(new SimpleDateFormat(type).format(timeLong));
			}
			else{
				if(isShowDefault){
					pageContext.getOut().write(new SimpleDateFormat(type).format(new Date()));
				}else{
					if(isView){
						pageContext.getOut().write("N/A");
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return super.doStartTag();
	}
}